package com.ruoyi.factory.bom.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.factory.bom.domain.BomCost;
import com.ruoyi.factory.bom.domain.BomTreeSelect;
import com.ruoyi.factory.bom.mapper.BomCostMapper;
import com.ruoyi.factory.bom.service.IBomCostService;
import com.ruoyi.factory.bom.service.IUpdateMaterialPriceService;
import com.ruoyi.factory.common.domain.AjaxBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * bom模型Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-30
 */
@Service
public class BomCostServiceImpl implements IBomCostService
{
    @Autowired
    private BomCostMapper bomCostMapper;

    @Autowired
    private IUpdateMaterialPriceService updateMaterialPriceService;

    /**
     * 查询bom模型
     *
     * @param id bom模型主键
     * @return bom模型
     */
    @Override
    public BomCost selectBomCostById(String id)
    {
        return bomCostMapper.selectBomCostById(id);
    }

    /**
     * 查询bom模型列表
     *
     * @param bomCost bom模型
     * @return bom模型
     */
    @Override
    public List<BomCost> selectBomCostList(BomCost bomCost)
    {
        return bomCostMapper.selectBomCostList(bomCost);
    }

    @Override
    public List<BomTreeSelect> buildBomCostTreeSelect(List<BomCost> bomCosts)
    {
        List<BomCost> bomCostTrees = buildBomCostTree(bomCosts);
        return bomCostTrees.stream().map(BomTreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<BomCost> buildBomCostTreeSelect2(List<BomCost> bomCosts)
    {
        List<BomCost> bomCostTrees = buildBomCostTree(bomCosts);
        return bomCostTrees;
    }

    /**
     * 构建前端所需要树结构
     *
     * @param
     * @return 树结构列表
     */
    @Override
    public List<BomCost> buildBomCostTree(List<BomCost> bomCosts)
    {
        List<BomCost> returnList = new ArrayList<BomCost>();
        List<String> tempList = new ArrayList<String>();
        for (BomCost bomCost : bomCosts) {
            tempList.add(bomCost.getPartId());
        }
        for (BomCost bomCost : bomCosts) {
            if (!tempList.contains(bomCost.getParentId()))
            {
                recursionFn(bomCosts, bomCost);
                returnList.add(bomCost);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = bomCosts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<BomCost> list, BomCost t)
    {
        // 得到子节点列表
        List<BomCost> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BomCost tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BomCost> list, BomCost t)
    {
        return getChildList(list, t).size() > 0;
    }
    /**
     * 得到子节点列表
     */
    private List<BomCost> getChildList(List<BomCost> list, BomCost t)
    {
        List<BomCost> tlist = new ArrayList<BomCost>();
        Iterator<BomCost> it = list.iterator();
        while (it.hasNext())
        {
            BomCost n = (BomCost) it.next();
//            String partId = t.getPartId();
//            String parentId = n.getParentId();
//            boolean equals = partId.equals(parentId);
//            System.out.println("是否相等"+equals);
//            System.out.println("n---"+parentId);
//            System.out.println("t---"+partId);
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getPartId()) && n.getBomName().equals(t.getBomName()) && n.getBomVar().equals(t.getBomVar()))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 新增bom模型
     *
     * @param bomCost bom模型
     * @return 结果
     */
    @Override
    public int insertBomCost(BomCost bomCost)
    {
        bomCost.setCreateTime(DateUtils.getNowDate());
        return bomCostMapper.insertBomCost(bomCost);
    }

    /**
     * 修改bom模型
     *
     * @param bomCost bom模型
     * @return 结果
     */
    @Override
    public int updateBomCost(BomCost bomCost)
    {
        bomCost.setUpdateTime(DateUtils.getNowDate());
        AjaxBean upgrtopr = updateMaterialPriceService.upgrtopr(bomCost);
        bomCost = (BomCost)upgrtopr.getData();
        return bomCostMapper.updateBomCost(bomCost);
    }




    /**
     * 批量删除bom模型
     *
     * @param ids 需要删除的bom模型主键
     * @return 结果
     */
    @Override
    public int deleteBomCostByIds(String[] ids)
    {
        return bomCostMapper.deleteBomCostByIds(ids);
    }

    /**
     * 删除bom模型信息
     *
     * @param id bom模型主键
     * @return 结果
     */
    @Override
    public int deleteBomCostById(String id)
    {
        return bomCostMapper.deleteBomCostById(id);
    }
}
